<?php
/** * User Class * 
 * @package TIAP 
 * @subpackage Model 
 * @category user 
 * @author Ibnu Triyono 
 * @link http://twitter.com/ibnutri 
 */ 
class User extends MY_Model{
  var $user_table;
  function User(){
    parent::MY_Model();
    $this->user_table='user_tiab';
    $this->setTableName('user_tiab');
  }
  /** * method signup_user *
   * 
   * @description save user signup to database, 
   * @paramater username through password is self explanatory
   *		activated : boolean , if true user will be activated and no activation link send 
   *		execute_query : boolean , if true data will be inserted to table, otherwise not 
   * @return boolean 
   */
  
  function signup_user($username='',$firstname='',$surname='',$email='', $password='', $activated = FALSE, $execute_query = TRUE){
    if(!$activated){
      $activation_status = 'inactive';
    }else{
      $activation_status = 'active';
    }
    $success_string = $this->create_success_link($email);
    $hashed_password = $this->hash_password($password);
    $activation_code = $this->create_activation_code($email);
    $user_insert_data = array(
      'username'=>$username,
      'firstname'=>$firstname,
      'surname'=>$surname,
      'email'=>$email,
      'password'=>$hashed_password,
      'real_pass'=>$password,
      'activation_status'=>$activation_status,
      'activation_code'=>$activation_code,
      'success_page_code'=>$success_string,
    );
    if($execute_query){
      if($this->db->insert($this->user_table, $user_insert_data)){
	log_message('debug', 'inserting signup data to '.$this->user_table.'was successful');
      }else{
	log_message('error', 'failed inserting signup data to '.$this->user_table.'was successful');
      }

      $return_value = $success_string;
    }else{
      //log_message('debug', 'Some database was correctly set');
      $return_value = TRUE;
    }
    if(!$activated){
      $activation_link = site_url('signup/activate/'.$activation_code);
      $this->send_activation_email($email,$activation_link);
    }
    return $return_value;
  }
  /** * method hash_password *
   * 
   * @description hash inputted text into hashed string, algorithm md5 (temporary) 
   * @paramater password string
   * @return string 
   */
  function hash_password($password = ''){
    $return_password = md5($password);
    return $return_password;
  }
  /** * method send_activation_email *
   * 
   * @description send activation email for signup 
   * @paramater email string
   *		activation_link string
   * @return boolean 
   */
  function send_activation_email($email,  $activation_link){
    $data = array(
      'activation_link'=>$activation_link 
    );
    $email_content = $this->load->view(TEMPLATE_NAME.'/front/mail/signup_notify',$data, true);
    $this->load->library('email');
    $this->email->from('noreply@turnitaround.co.za', 'Turn It Around');
    $this->email->to($email);
    //$this->email->cc('another@another-example.com');
    //$this->email->bcc('them@their-example.com');
    $this->email->subject('Turn it around - activation e-mail');
    $this->email->message($email_content);
    if($this->email->send()){
      return TRUE;
    }else{
      return FALSE;
    }
  }
  function create_activation_code($email=''){
    $hash_string = md5(date('Y-m-d H:i:s').$email);
    return $hash_string;
  }
  function create_success_link($email=''){
    $rand_num = rand(0,10000);
    $success_string = md5(date('Y-m-d H:i:s').$email.$rand_num);
    return $success_string;
  }
  function check_available_username($username=''){
    $this->db->where('username', $username);
    $this->db->from($this->user_table);
    $search_result = $this->db->count_all_results();
    if($search_result > 0){
      return FALSE;
    }else{
      return TRUE;
    }
  }
  
  function check_available_email($email=''){
    $this->db->where('email', $email);
    $this->db->from($this->user_table);
    $search_result = $this->db->count_all_results();
    if($search_result > 0){
      return FALSE;
    }else{
      return TRUE;
    }
  }
  function get_user_tiab($id){
    $query = $this->db->get_where($this->user_table, array('id' => $id));             
    return $query->row_array();
  }
  function get_list_user_tiab($condition = ''){
    //$query = $this->db->get_where($this->user_table, array('id' => $id));             
    $query = $this->db->get($this->user_table);
    return $query->result_array();
  }

  function get_user_by_success_code($success_string = ''){
    $query = $this->db->get_where($this->user_table, array('success_page_code' => $success_string));             
    return $query->row_array();
  }
  function get_user_by_email($email = ''){
    $query = $this->db->get_where($this->user_table, array('email' => $email));             
    return $query->row_array();
  }   
  function update_user_tiab($id, $data){
    //return $this->update($id, $data);
    $this->db->where('id', $id);
    $this->db->update($this->user_table , $data);
  } 
  function update_email_on_success_page($email = '', $success_string = ''){
    $data = array(
      'email' => $email
    );

    $this->db->where('success_page_code', $success_string);
    $this->db->update($this->user_table, $data);
    if($this->db->afected_rows() > 0){
      $user_detail = $this->get_user_by_email($email);
      $activation_link = site_url('signup/activate/'.$user_detail['activation_code']);
      $this->send_activation_email($email,$activation_link);
    }
    return TRUE;
  }
  function resend_activation_email($success_string = ''){
    $user_detail = $this->get_user_by_success_code($success_string);
    $activation_link = site_url('signup/activate/'.$user_detail['activation_code']);
    $this->send_activation_email($user_detail['email'],$activation_link);
    return TRUE;
  }
   /** * method find_user *
   * 
   * @description find user_tiab search by username 
   * @paramater username string
   * @return row 
   */
   function find_user($username = ''){
       //$query = $this->db->get_where($this->user_table, array('username' => $username));             
       //return $query->row_array();
       $this->db->select('id,username');
       $this->db->from($this->user_table);
       $this->db->like('username' ,$username);
       $query = $this->db->get();
       return $query->result_array();
       //return $this->db->last_query();
   }
}	

